﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using IntelligentDecisioning.SharePoint.Definition.Fields;
using System.Collections.Specialized;

namespace IntelligentDecisioning.SharePoint.Definition
{
    /// <summary>
    /// 
    /// Used to define a view on a sharepoint list.
    /// 
    /// Fields required in the view may be specified by either adding their 
    /// inteneral name to the InternalFieldName collection or adding
    /// the ListField class directly to the Fields collection (useful if the
    /// field has just been added to the list in code). In either case the 
    /// field must already exist on the list, id-sharepoint will not provision
    /// a new field to a list because it has been added to a view.
    /// 
    /// Views may optionally specify a CAML filter to be applied to select the
    /// items required in the view and the number of items to display before the view
    /// is paged.
    /// 
    /// </summary>
    public class SharePointListView
    {
        #region Data

        public String ViewName { get; set; }
        public List<String> InternalFieldNames { get; set; }
        public List<Field> Fields { get; set; }
        public String Filter { get; set; }
        public uint PageSize { get; set; }
        public bool IsDefaultView { get; set; }
        public SPViewCollection.SPViewType ViewType { get; set; }
        public bool IsPersonalView { get; set; }

        #endregion

        #region Constructor

        public SharePointListView(String viewName)
        {
            this.ViewName = viewName;
            InternalFieldNames = new List<String>();
            Fields = new List<Field>();
            Filter = String.Empty;
            PageSize = 25;
            IsDefaultView = false;
            IsPersonalView = false;
            ViewType = SPViewCollection.SPViewType.Html;
        }

        #endregion

        #region Methods

        public bool HasFilter()
        {
            return (String.IsNullOrEmpty(Filter)) ? false : true;
        }

        internal StringCollection GetAllInternalFieldNames()
        {
            StringCollection allInternalFieldNames = new StringCollection();
            allInternalFieldNames.AddRange(InternalFieldNames.ToArray());

            foreach (Field field in Fields)
            {
                allInternalFieldNames.Add(field.StaticName);
            }

            return allInternalFieldNames;
        }

        #endregion
    }
}
